﻿using Dapper;
using JX.Core;
using JX.Core.Entity;
using JX.Infrastructure;
using JX.Infrastructure.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace JX.Application
{
	/// <summary>
	/// 用户资金:为用户，用户组添加资金并记录日志
	/// </summary>
	public class UserMoneyServiceAppDapper : IUserMoneyServiceAppDapper
	{
		private string m_FromUser = MyHttpContext.CurrentUserName;
		private string m_FromUserIp = Utility.GetClientIP();
		private readonly IUsersRepositoryDapper _usersRepository;
		private readonly IBankrollLogRepositoryDapper _bankrollLogRepository;
		/// <summary>
		/// 仓储接口
		/// </summary>
		/// <param name="usersRepository"></param>
		/// <param name="bankrollLogRepository"></param>
		public UserMoneyServiceAppDapper(IUsersRepositoryDapper usersRepository, IBankrollLogRepositoryDapper bankrollLogRepository)
		{
			_usersRepository = usersRepository;
			_bankrollLogRepository = bankrollLogRepository;
		}

		/// <summary>
		/// 为所有会员增加
		/// </summary>
		/// <param name="howMany">正数为增加的数量，负数为减少的数量</param>
		/// <param name="reason">备注</param>
		/// <param name="memo">内部记录</param>
		/// <returns></returns>
		public bool IncreaseForAll(decimal howMany, string reason,string memo)
		{
			string strColumns = "Balance = isnull(Balance,0) + @Balance ";
			string strWhere = "";
			var parameters = new DynamicParameters();
			parameters.Add("Balance", howMany);
			bool flag = _usersRepository.Update(strColumns, strWhere, parameters);
			if (flag)
			{
				var userList = _usersRepository.GetList(writeAndRead: WriteAndReadEnum.Write);
				AddLog(howMany, userList, reason, memo);
			}
			return flag;
		}
		/// <summary>
		/// 为所有会员增加
		/// </summary>
		/// <param name="howMany">正数为增加的数量，负数为减少的数量</param>
		/// <param name="reason">备注</param>
		/// <param name="memo">内部记录</param>
		/// <returns></returns>
		public async Task<bool> IncreaseForAllAsync(decimal howMany, string reason, string memo)
		{
			string strColumns = "Balance = isnull(Balance,0) + @Balance ";
			string strWhere = "";
			var parameters = new DynamicParameters();
			parameters.Add("Balance", howMany);
			bool flag = await _usersRepository.UpdateAsync(strColumns, strWhere, parameters);
			if (flag)
			{
				var userList = _usersRepository.GetList(writeAndRead: WriteAndReadEnum.Write);
				AddLog(howMany, userList, reason, memo);
			}
			return flag;
		}

		/// <summary>
		/// 为指定的会员组增加
		/// </summary>
		/// <param name="groups">会员组ID集合</param>
		/// <param name="howMany">正数为增加的数量，负数为减少的数量</param>
		/// <param name="reason">备注</param>
		/// <param name="memo">内部记录</param>
		/// <returns></returns>
		public bool IncreaseForGroup(string groups, decimal howMany, string reason, string memo)
		{
			if (!DataValidator.IsValidId(groups))
			{
				return false;
			}
			string strColumns = "Balance = isnull(Balance,0) + @Balance";
			string strWhere = "GroupId in @ids";
			var parameters = new DynamicParameters();
			parameters.Add("Balance", howMany);
			parameters.Add("ids", StringHelper.GetArrayBySplit<int>(groups).ToArray());
			bool flag = _usersRepository.Update(strColumns, strWhere, parameters);
			if (flag)
			{
				var userList = _usersRepository.GetList(strWhere, parameters, writeAndRead: WriteAndReadEnum.Write);
				AddLog(howMany, userList, reason, memo);
			}
			return flag;
		}
		/// <summary>
		/// 为指定的会员组增加
		/// </summary>
		/// <param name="groups">会员组ID集合</param>
		/// <param name="howMany">正数为增加的数量，负数为减少的数量</param>
		/// <param name="reason">备注</param>
		/// <param name="memo">内部记录</param>
		/// <returns></returns>
		public async Task<bool> IncreaseForGroupAsync(string groups, decimal howMany, string reason, string memo)
		{
			if (!DataValidator.IsValidId(groups))
			{
				return false;
			}
			string strColumns = "Balance = isnull(Balance,0) + @Balance";
			string strWhere = "GroupId in @ids";
			var parameters = new DynamicParameters();
			parameters.Add("Balance", howMany);
			parameters.Add("ids", StringHelper.GetArrayBySplit<int>(groups).ToArray());
			bool flag = await _usersRepository.UpdateAsync(strColumns, strWhere, parameters);
			if (flag)
			{
				var userList = _usersRepository.GetList(strWhere, parameters, writeAndRead: WriteAndReadEnum.Write);
				AddLog(howMany, userList, reason, memo);
			}
			return flag;
		}

		/// <summary>
		/// 为指定的会员名增加
		/// </summary>
		/// <param name="toUser">会员名集合</param>
		/// <param name="howMany">正数为增加的数量，负数为减少的数量</param>
		/// <param name="reason">备注</param>
		/// <param name="memo">内部记录</param>
		/// <returns></returns>
		public bool IncreaseForUsers(string toUser, decimal howMany, string reason, string memo)
		{
			if (string.IsNullOrEmpty(toUser))
			{
				return false;
			}
			string strColumns = "Balance = isnull(Balance,0) + @Balance";
			string strWhere = "UserName in @ids";
			var parameters = new DynamicParameters();
			parameters.Add("Balance", howMany);
			parameters.Add("ids", StringHelper.GetArrayBySplit<string>(toUser).ToArray());
			bool flag = _usersRepository.Update(strColumns, strWhere, parameters);
			if (flag)
			{
				var userList = _usersRepository.GetList(strWhere, parameters, writeAndRead: WriteAndReadEnum.Write);
				AddLog(howMany, userList, reason, memo);
			}
			return flag;
		}
		/// <summary>
		/// 为指定的会员名增加
		/// </summary>
		/// <param name="toUser">会员名集合</param>
		/// <param name="howMany">正数为增加的数量，负数为减少的数量</param>
		/// <param name="reason">备注</param>
		/// <param name="memo">内部记录</param>
		/// <returns></returns>
		public async Task<bool> IncreaseForUsersAsync(string toUser, decimal howMany, string reason, string memo)
		{
			if (string.IsNullOrEmpty(toUser))
			{
				return false;
			}
			string strColumns = "Balance = isnull(Balance,0) + @Balance";
			string strWhere = "UserName in @ids";
			var parameters = new DynamicParameters();
			parameters.Add("Balance", howMany);
			parameters.Add("ids", StringHelper.GetArrayBySplit<string>(toUser).ToArray());
			bool flag = await _usersRepository.UpdateAsync(strColumns, strWhere, parameters);
			if (flag)
			{
				var userList = _usersRepository.GetList(strWhere, parameters, writeAndRead: WriteAndReadEnum.Write);
				AddLog(howMany, userList, reason, memo);
			}
			return flag;
		}

		/// <summary>
		/// 为指定的会员ID增加
		/// </summary>
		/// <param name="toUser">会员ID集合</param>
		/// <param name="howMany">正数为增加的数量，负数为减少的数量</param>
		/// <param name="reason">备注</param>
		/// <param name="memo">内部记录</param>
		/// <returns></returns>
		public bool IncreaseForUserID(string toUser, decimal howMany, string reason, string memo)
		{
			if (!DataValidator.IsValidId(toUser))
			{
				return false;
			}
			string strColumns = "Balance = isnull(Balance,0) + @Balance";
			string strWhere = "UserID in @ids";
			var parameters = new DynamicParameters();
			parameters.Add("Balance", howMany);
			parameters.Add("ids", StringHelper.GetArrayBySplit<int>(toUser).ToArray());
			bool flag = _usersRepository.Update(strColumns, strWhere, parameters);
			if (flag)
			{
				var userList = _usersRepository.GetList(strWhere, parameters, writeAndRead: WriteAndReadEnum.Write);
				AddLog(howMany, userList, reason, memo);
			}
			return flag;
		}
		/// <summary>
		/// 为指定的会员ID增加
		/// </summary>
		/// <param name="toUser">会员ID集合</param>
		/// <param name="howMany">正数为增加的数量，负数为减少的数量</param>
		/// <param name="reason">备注</param>
		/// <param name="memo">内部记录</param>
		/// <returns></returns>
		public async Task<bool> IncreaseForUserIDAsync(string toUser, decimal howMany, string reason, string memo)
		{
			if (!DataValidator.IsValidId(toUser))
			{
				return false;
			}
			string strColumns = "Balance = isnull(Balance,0) + @Balance";
			string strWhere = "UserID in @ids";
			var parameters = new DynamicParameters();
			parameters.Add("Balance", howMany);
			parameters.Add("ids", StringHelper.GetArrayBySplit<int>(toUser).ToArray());
			bool flag = await _usersRepository.UpdateAsync(strColumns, strWhere, parameters);
			if (flag)
			{
				var userList = _usersRepository.GetList(strWhere, parameters, writeAndRead: WriteAndReadEnum.Write);
				AddLog(howMany, userList, reason, memo);
			}
			return flag;
		}

		/// <summary>
		/// 添加资金日志：记录BankrollLogInfo明细日志
		/// </summary>
		/// <param name="money"></param>
		/// <param name="userList"></param>
		/// <param name="reason"></param>
		/// <param name="memo"></param>
		private void AddLog(decimal money, IEnumerable<UsersEntity> userList, string reason, string memo)
		{
			foreach (var info in userList)
			{
				var bankrollItemInfo = new BankrollLogEntity();
				bankrollItemInfo.UserName = info.UserName;
				bankrollItemInfo.ClientID = info.ClientID;
				bankrollItemInfo.Money = money;
				bankrollItemInfo.MoneyType = 4;
				bankrollItemInfo.CurrencyType = 3;
				bankrollItemInfo.DateAndTime = new DateTime?(DateTime.Now);
				bankrollItemInfo.Remark = reason;
				bankrollItemInfo.Inputer = string.IsNullOrEmpty(m_FromUser) ? "System" : m_FromUser;
				bankrollItemInfo.IP = m_FromUserIp;
				bankrollItemInfo.LogTime = new DateTime?(DateTime.Now);
				bankrollItemInfo.Status = (int)BankrollItemStatus.Confirm;
				bankrollItemInfo.Memo = memo;
				bankrollItemInfo.EBankID = 0;
				bankrollItemInfo.Bank = "";
				bankrollItemInfo.OrderID = 0;
				bankrollItemInfo.PaymentID = 0;
				_bankrollLogRepository.Add(bankrollItemInfo);
			}
		}

		

		

		

		
	}
}
